<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\Rbac;

use App\Controller\Admin\AdminController;
use App\Model\RbacRoute;
use App\Repositories\AdminRepository;
use App\Repositories\RbacRoleRepository;
use App\Repositories\RbacRouteRepository;
use App\Request\Rbac\StoreRoleRequest;
use App\Request\Rbac\StoreRouteRequest;
use App\Request\StoreAdminRequest;
use Hyperf\Di\Annotation\Inject;

class RouteController extends AdminController{

    public function consts(){
        $types = RbacRoute::getLabels('type');
        $return=[];
        $return['types'] = [];
        foreach ($types as $key=>$val){
            $obj=[
                'name' => $val,
                'value' => $key
            ];
            $return['types']=array_merge($return['types'],[$obj]);
        }
        return $this->response->success($return);
    }

    public function all(){
        $search['id']=[
            'field' => 'id',
            'value' => '1',
            'method' => '!='
        ];
        $datas = AdminRepository::getInstance()->getList('*',$search,false);
        return $this->response->success($datas);
    }

    public function list(){
        $rows = $this->request->input('rows',10);
        $type = $this->request->input('type','');
        $search = [];
        $search['type']=[
            'field' => 'type',
            'value' => $type
        ];
        $datas = RbacRouteRepository::getInstance()->getList('*',$search,['limit'=>$rows]);
        return $this->response->success($datas);
    }

    public function store(StoreRouteRequest $request){
        $params = $request->all();
        //return $this->response->success($params);
        $result = RbacRouteRepository::getInstance()->store($params);
        if(!$result['success']){
            return $this->response->error(10055,$result['msg']);
        }
        return $this->response->success([
            'id' => $result['data']->id
        ]);
    }

    public function detail(){

    }

    public function delete(){

    }

    public function changeField()
    {
        $field = $this->request->input('field');
        $value = $this->request->input('value',false);
        $id = $this->request->input('id');
        $update =[
            $field=>$value=='true'?1:0
        ];
        $where=[
            [
                'field'=>'id',
                'value'=>$id
            ]
        ];
        $return = AdminRepository::getInstance()->update($update,$where);
        return $this->response->success([]);
    }
}